package com.easy.product.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.common.bean.Product;
import com.easy.common.service.IProductService;
import com.easy.common.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("product")
public class ProductController {
    @Autowired
    IProductService productService;
    @PostMapping("/")
    public ResultData insert(@RequestBody Product product){
        Product result=productService.insert(product);
        return ResultData.success(result);
    }
    @PutMapping("/")
    public ResultData update(@RequestBody Product product){
        return ResultData.success(productService.update(product));
    }
    @DeleteMapping("/{id}")
    public ResultData delete(@PathVariable Integer id){
        int result=productService.delete(id);
        if(result>=1){
            return ResultData.success();
        }else{
            return ResultData.fail();
        }
    }
    @GetMapping("/{id}")
    //@PreAuthorize("hasAnyAuthority('user:list')")
    public ResultData getByID(@PathVariable Integer id){
        return ResultData.success(productService.getByID(id));
    }
    @GetMapping("/page")
    public ResultData getListByPage(Page page, Product product){
        IPage<Product> data=productService.getListByPage(page,product);
        return ResultData.success(data);
    }
    @GetMapping("/list")
    //@PreAuthorize("hasAnyAuthority('user:page')")
    public ResultData getList(Product product){
        List<Product> data=productService.getList(product);
        return ResultData.success(data);
    }
}
